﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhaoXi.MSACommerce.CommonServiceClient.AspNetCore;
using ZhaoXi.MSACommerce.Consul.ServiceDiscovery;

namespace ZhaoXi.MSACommerce.CommonServiceClient
{
    public static class ServiceClientExtension
    {
        public static void AddServiceClient<TServiceClient>(this IServiceCollection services,
            Action<ServiceClientOption> configureServiceClient,
            Action<HttpClient> configureHttpClient)
            where TServiceClient : class, ISeviceClient
        {
            var serviceClientOption = new ServiceClientOption();
            configureServiceClient.Invoke(serviceClientOption);

            services.AddConsulDiscovery();

            services.AddLoadBalancer<TServiceClient>(serviceClientOption.LoadBalancingStrategy);

            services.AddHttpClient<TServiceClient>(configureHttpClient);

            services.AddScoped<TServiceClient>();
        }
    }
}
